﻿using System.Web;

namespace ML.Entity
{
    /// <summary>
    /// 封装用户读取设置取消Session
    /// </summary>
    public static class UserHelper
    {
        /// <summary>
        /// 保存用户记录的Session信息,用户登录信息
        /// </summary>
        public static void SetSession(UserInfoSession userInfoSession)
        {
            HttpContext.Current.Session["UserInfo"] = userInfoSession;
        }

        /// <summary>
        /// 从Session中获取当前登录用户信息
        /// </summary>
        public static UserInfoSession GetCurrentUser()
        {
            var obj = HttpContext.Current.Session["UserInfo"];
            return obj as UserInfoSession;
        }

        /// <summary>
        /// 移除当前登录用户
        /// </summary>
        public static void RemoveCurrentUser()
        {
            HttpContext.Current.Session.Remove("UserInfo");
        }

        /// <summary>
        /// 读取当前登录用户Id
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserId()
        {
            UserInfoSession userInfoSession = GetCurrentUser();
            if (userInfoSession == null)
            {
                return "";
            }
            return userInfoSession.UserId;
        }
    }
}